<template>
    <el-drawer append-to-body v-model="drawer" :title="`出库明细【${detailsInfo.obDetailId}】任务详情`" direction="rtl" size="60%">
        <wms-form v-model:query-params="queryParams" :form-list="formList" @query="getList" label-width="88px"
            @export="handleExport">
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['wms:task:remove']">取消</el-button>
            </el-col>
        </wms-form>
        <wms-table :loading="loading" :data-list="taskList" @selection-change="selectionChange" :single="false"
            :column="column" :pageInfo="queryParams" @get-list="getList" />
    </el-drawer>
</template>

<script setup name="Task">
import { listTask, getTask, cancelTask, addTask, updateTask } from "@/api/wms/task";
import WmsTable from '@/components/WmsTable/index.vue'
import WmsForm from '@/components/WmsForm/index.vue'
import { data, column, formList } from './index.js'
const dataReactive = reactive(data);
const { proxy } = getCurrentInstance();
const queryParams = reactive(data.queryParams);
const props = defineProps({
    detailsInfo: {
        type: Object,
        require: true
    }
})
let drawer = defineModel()
const taskList = ref([]);
const selectedTasks = ref([]);
const single = ref(true);
const loading = ref(true);
const showSearch = ref(true);

/** 查询任务查询列表 */
function getList() {
    console.log("查询出库任务列表,props.detailsInfo", props.detailsInfo);
    queryParams.masterId = props.detailsInfo.obMasterId
    queryParams.sourceDetail = props.detailsInfo.obDetailId
    loading.value = true;
    listTask(queryParams).then(response => {
        taskList.value = response.rows;
        queryParams.total = response.total;
        loading.value = false;
    });
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    queryParams.dateRange = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function selectionChange(selection) {
    console.log("选中的数据:", selection);
    single.value = selection.length != 1;
    selectedTasks.value = selection;
}

const multiple = computed(() => !selectedTasks.value.length)

const taskIds = computed(() => selectedTasks.value.map(item => item.taskId))

/** 取消按钮操作 */
function handleDelete(row) {
    proxy.$modal.confirm('是否确认取消选中任务？').then(function () {
        return cancelTask({ ids: taskIds.value });
    }).then((res) => {
        if (res.code == 200) {
            proxy.$modal.msgSuccess("任务取消成功");
            getList();
        } else {
            proxy.$modal.msgError(res.msg);
        }
    }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('wms/task/export', {
        ...queryParams
    }, `task_${new Date().getTime()}.xlsx`)
}

getList();
</script>
